define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Purchase, $window, $timeout, $ionicSlideBoxDelegate,GoPage) {
		$scope.input = new Object();
		$scope.input.nPage = 1;
		
		//页面跳转
		$scope.onshow_flashsale = function(id){
			GoPage.go_states("flashsale",{id:id});
		}
		//统计
		$scope.onshow_statistics = function(id){
			GoPage.go_states("statistics",{"id":id,type:2});
		}

		$scope.isLoad = false; //可以加载
		//画面加载
		$scope.$on('$ionicView.afterEnter', function() {
			//动态设置高度
			var all = $window.innerHeight;
			//计算滚动条
			$scope.myObj = {
				"height": all - 44 + "px"
			};
		}, false);
		//滑动时候触发
		$scope.selectIndex = 0;
		$scope.slideChanged = function(index) {
			//手指滑动
			$scope.selectIndex = index;
		};
		$scope.activeSlide = function(index) {
			$scope.selectIndex = index;
			$ionicSlideBoxDelegate.$getByHandle('main-handle').slide(index);
		};
		
		$scope.isLoad = false; //可以加载
		//下拉刷新 判断哪个模块进行刷新
		$scope.doRefresh = function() {
			//防止重复加载
			$scope.isLoad = true;
			Purchase.get_list(1,1,function(data) {
				if(data!=null){
					$scope.list = data;
				}else{
					$scope.list = new Object();					
				}
				$scope.input.nPage = 1;
			},
			function() {
				$scope.$broadcast('scroll.refreshComplete');
				$timeout(function() {
				$scope.isLoad = false;
			}, 1500);
			});
		};
		//加载更多
		$scope.doRefreshMore = function() {
			//防止重复加载
			$scope.isLoad = true;
			if($scope.selectIndex == 0){
				Purchase.get_list(1,$scope.input.nPage+1,function(data) {
					if(data!=null){
						if($scope.list!=undefined){
							$scope.list.doing = $scope.list.doing.concat(data.doing);
							$scope.input.nPage += 1;
						}
						else{
							$scope.list.doing = data.doing;
						}	
					}
				},
				function() {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
				
				});
			}
			if($scope.selectIndex == 1){
				Purchase.get_list(1,$scope.input.nPage+1,function(data) {
					if(data!=null){
						if($scope.list!=undefined){
							$scope.list.pending = $scope.list.pending.concat(data.pending);
							$scope.input.nPage += 1;
						}
						else{
							$scope.list.pending = data.pending;
						}	
					}
				},
				function() {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
				
				});
			}
			if($scope.selectIndex == 2){
				Purchase.get_list(1,$scope.input.nPage+1,function(data) {
					if(data!=null){
						if($scope.list!=undefined){
							$scope.list.ended = $scope.list.ended.concat(data.ended);
							$scope.input.nPage += 1;
						}
						else{
							$scope.list.ended = data.ended;
						}	
					}
				},
				function() {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
				
				});
			}
		};
		$scope.init_data = function(){
			Purchase.get_list(1,1,function(data){
				$scope.list = data;
			})
		}
		$scope.init_data();
	}

	controller.$inject = ['$scope', 'Purchase', '$window', '$timeout', '$ionicSlideBoxDelegate','GoPage'];
	
	return app.registerController("purchasectrl",controller);
});